//
//  BiotopeJionVC.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/9/12.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SVProgressHUD

class BiotopeJionVC: BaseViewController {
    
    var dataModels : [BiotopeModel]?

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "选择社区"
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "加入", style: .plain, target: self, action: #selector(dissMissYou))
        
        plainTableView.register(UINib(nibName: StringFromClass(classType: BiotopeSeletCell.self), bundle: nil), forCellReuseIdentifier: BiotopeSeletCell.identifer())
        
        plainTableView.tableFooterView = UIView()
        
        weak var weakSelf = self
        
        plainTableView.mj_header =  ZMRefreshHeader.init {
            
            weakSelf!.loadData()
            
        }
        
        plainTableView.mj_header.beginRefreshing()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    
    func dissMissYou() {
        
        
        NetworkTools.shared.sendRequest(method: .GET_METHOD, pathStr: "mobile/index.php?c=member_index&a=member_state", parameters: nil) { (responseObject, error) in
            
            if error == nil{
                
                
                UserDefaults.standard.set("TRUE", forKey: "IsNewCityUser")
                
                UIApplication.shared.keyWindow?.rootViewController = TabBarController()
  
            
        }
        
        
        
    }
    
    

   
}


}

extension BiotopeJionVC{
    
    func loadData() {
        NetworkTools.shared.sendRequest(method: .GET_METHOD, pathStr: "mobile/index.php?c=index&a=get_bank_list", parameters: nil) { [weak self] (responseObject, error)  in
            
            
            if error == nil {
                
                self?.dataModels = [BiotopeModel]()
//                for dic in responseObject!["datas"]!["list"] as! [[String : AnyObject]]{
//                    
//                    self!.dataModels!.append("东津世纪城")
//                    
//                }
                let temp1 = BiotopeModel()
                temp1.name = "东津世纪城"
                self!.dataModels!.append(temp1)
                
                let temp2 = BiotopeModel()
                temp2.name = "民发世纪城"
                self!.dataModels!.append(temp2)

                
                self!.plainTableView.mj_header.endRefreshing()
                
                self!.plainTableView.reloadData()
                
                
            }else{
                
                self!.plainTableView.mj_header.endRefreshing()
                
                SVProgressHUD.showError(withStatus: error?.domain)
                
                
            }
            
            
        }
        
    }
    
    
    
    
    
}







// MARK:- tableview代理方法
extension BiotopeJionVC{
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        if dataModels == nil {
            return 0
        }
        
        return 1
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if dataModels == nil {
            return 0
        }
        
        return dataModels?.count ?? 0
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let tableCell : BiotopeSeletCell =  tableView.dequeueReusableCell(withIdentifier: BiotopeSeletCell.identifer()) as! BiotopeSeletCell
        
        tableCell.model = dataModels?[indexPath.row]
        
        return tableCell
        
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 46
    }
    
    
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //        super.tableView(tableView, didSelectRowAt: indexPath)
        
        let model = dataModels![indexPath.row]
        
        if  model.isSelect  == true {
            return
        }
        
        
        for item in dataModels! {
            item.isSelect = false
        }
        
        model.isSelect = true
        
        
        plainTableView.reloadData()
        
        
        
    }
    
    
    
}



